// pages/equipment/equipment.js
const httpRequest = require('../../utils/http_utils.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    open: false,
    show: false,
    // mark 是指原点x轴坐标
    mark: 0,
    // newmark 是指移动的最新点的x轴坐标 
    newmark: 0,
    istoright: true,

    title: '设备信息',
    listArray: [], //数据集合
    totalNum: 0, //数据总条数
    pageSize: 15, //每页显示条数
    counter: 1, //计数器
    uploading: false,
    downloading: false,
    noMore: false,
    loadingFailed: false,
    scrollTop: 0,
    index: 0, //picker下标
    sys_Data: [{
      RowGuid: '0',
      ParentGuid: '0',
      SystemName: '请选择'
    }], //设备分类一
    sysType_Data: [{
      RowGuid: '0',
      SystemName: '请选择'
    }], //设备分类二
    indexTwo: 0, //分类二下标
    classify_Data: [{
      RowGuid: '0',
      ClassifyName: '请选择'
    }], //分类三
    indexThree: 0, //分类三下标
    ParentGuid: '',
    havefssb: '0', //是否有附属设备 1有
    RegionArray: [{
      RowGuid: '0',
      RegionName: '请选择',
      RegionCode: '00'
    }], //区域
    RegionIndex: 0,
    BuildingArray: [{
      RowGuid: '0',
      BuildingName: '请选择',
      BuildingCode: '00'
    }], //建筑
    BuildingIndex: 0,
    FloorArray: [{
      RowGuid: '0',
      FloorName: '请选择',
      FloorCode: '00'
    }], //楼层
    FloorIndex: 0,
    RoomArray: [{
      RowGuid: '0',
      Room: '请选择'
    }], //房间
    RoomIndex: 0,
    Region: {
      CoditionName: "RegionGuid",
      CoditionType: "nvarchar",
      CoditionValue: [""],
    }, //区域值
    Build: {
      CoditionName: "BuildingGuid",
      CoditionType: "nvarchar",
      CoditionValue: [""],
    }, //建筑值
    Floor: {
      CoditionName: "FloorGuid",
      CoditionType: "nvarchar",
      CoditionValue: [""],
    }, //楼层值
    Rooms: {
      CoditionName: "RoomGuid",
      CoditionType: "nvarchar",
      CoditionValue: [""],
    }, //房间值
    SysInfo: {
      CoditionName: "SysGuid",
      CoditionType: "nvarchar",
      CoditionValue: [""],
    }, //设备一值
    SysType: {
      CoditionName: "Sys_TypeGuid",
      CoditionType: "nvarchar",
      CoditionValue: [""],
    }, //设备二值
    Classify: {
      CoditionName: "ClassifyGuid",
      CoditionType: "nvarchar",
      CoditionValue: [""],
    }, //设备三值
    EquipmentName: {
      CoditionName: "EquipmentName",
      CoditionType: "nvarchar",
      CoditionValue: [""],
    }, //输入框值
    val: '', //输入框value
    showIs: false, //暂无图片
    customHeight: 0, //自定义高度

    CustomBar:0,
    a:null,
  },












  showModal(e) {
    // console.log(e.currentTarget.dataset.target)
    if (e.currentTarget.dataset.target == this.data.a){
      this.setData({
        modalName: null,
        a:null
      })
    }else{
      this.setData({
        modalName: e.currentTarget.dataset.target,
        a: e.currentTarget.dataset.target
      })
    }
  },
  hideModal(e) {
    this.setData({
      modalName: null,
      a:null
    })
  },























  initData: function() { //基础信息列表
    var that = this;
    var CoditionArray = new Array();
    CoditionArray.push(that.data.SysInfo);
    CoditionArray.push(that.data.SysType);
    CoditionArray.push(that.data.Classify);
    CoditionArray.push(that.data.Region);
    CoditionArray.push(that.data.Build);
    CoditionArray.push(that.data.Floor);
    CoditionArray.push(that.data.Rooms);
    CoditionArray.push(that.data.EquipmentName);
    var param = {
      rolename: '*',
      pagesize: that.data.pageSize,
      currentpage: that.data.counter,
      where: JSON.stringify(CoditionArray),
      ParentGuid: that.data.ParentGuid,
      orderby: 'order by OperateDate desc',
      type: 2
    }
    console.log(JSON.stringify(param) + "________________列表数据")
    httpRequest.GetValidateRequest(httpRequest.apiInfo.GetEquipmentSearchApi, param, function(data) {
      console.log(JSON.stringify(data) + "接口数据")
      console.log(data.pageData.length)
      var listDataArray = that.data.listArray
      // console.log('listDataArray', listDataArray)
      if (data.totalNum == 0) {
        that.setData({
          showIs: true,
          noMore: false
        })
      } else {
        for (var i = 0; i < data.pageData.length; i++) {
          listDataArray.push(data.pageData[i])
        }
        // console.log('listDataArray', listDataArray)
        //listDataArray=data.pageData
        that.setData({
          listArray: listDataArray,
          totalNum: data.totalNum,
          uploading: false,
          noMore: false,
          showIs: false
        })
      }
      that.equipmentOne();
      that.getRegion();
    }, function(err) {})
  },
  //下拉刷新
  scrollToUpper: function() {
    this.setData({
      uploading: true,
      noMore: false,
    })
    this.initData();
  },

  //上拉加载
  scrollToLower: function() {
    var that = this
    var pages = Math.round((that.data.totalNum / 10) * 100) / 100
    if (that.data.counter > pages) {
      that.setData({
        noMore: true
      })
      setTimeout(() => {
        that.setData({
          noMore: false
        })
      }, 2000)
    } else {
      that.setData({
        noMore: false,
        downloading: true
      })
      setTimeout(() => {
        that.setData({
          downloading: false
        })
      }, 3000)
      that.setData({
        counter: that.data.counter + 1
      })
      that.initData();
    }
  },

  bindPicker: function(e) { //获取下标
    this.setData({
      index: e.detail.value
    })
    this.data.SysInfo = {
      CoditionName: "SysGuid",
      CoditionType: "nvarchar",
      CoditionValue: [this.data.sys_Data[e.detail.value].RowGuid],
    }
    this.equipmentTwo();
  },

  equipmentOne: function() {
    var that = this;
    var params = {
      ParentGuid: '0'
    }
    httpRequest.GetValidateRequest(httpRequest.apiInfo.GetMasterSysDictionaryApi, params, function(data) {
      if (data.length > 0) {
        var newarr = that.data.sys_Data
        for (var i = 0; i < data.length; i++) {
          var param = {
            RowGuid: data[i].RowGuid,
            ParentGuid: data[i].ParentGuid,
            SystemName: data[i].SystemName
          }
          newarr.push(param)
        }
        that.setData({
          sys_Data: newarr
        })
      }
    }, function(err) {})
  },

  bindPickerTwo: function(e) { //获取下标

    this.setData({
      indexTwo: e.detail.value
    })
    this.data.SysType = {
      CoditionName: "Sys_TypeGuid",
      CoditionType: "nvarchar",
      CoditionValue: [this.data.sysType_Data[e.detail.value].RowGuid],
    }
    this.equipmentOneThree();
  },

  equipmentTwo: function() { //设备分类二
    var that = this;
    that.setData({
      sysType_Data: [{
        RowGuid: '0',
        SystemName: '请选择'
      }],
      indexTwo: 0
    })
    that.setData({
      classify_Data: [{
        RowGuid: '0',
        ClassifyName: '请选择'
      }],
      indexThree: 0
    })
    var index = that.data.index;
    var sys_Data = that.data.sys_Data;
    // console.log(sys_Data[index].RowGuid)
    if (sys_Data[index].SystemName == "请选择") {
      that.setData({
        sysType_Data: [{
          RowGuid: '0',
          SystemName: '请选择'
        }],
        indexTwo: 0
      })
    } else {
      var params = {
        ParentGuid: sys_Data[index].RowGuid
      }
      httpRequest.GetValidateRequest(httpRequest.apiInfo.GetMasterSysDictionaryApi, params, function(data) {
        console.log(JSON.stringify(data) + "_______________设备分类二")
        if (data.length > 0) {
          var towArr = that.data.sysType_Data
          for (var j = 0; j < data.length; j++) {
            var params = {
              RowGuid: data[j].RowGuid,
              SystemName: data[j].SystemName
            }
            towArr.push(params)
          }
          that.setData({
            sysType_Data: towArr
          })
        }
      }, function(err) {})
    }
  },

  bindPickerThree: function(e) { //获取下标

    this.setData({
      indexThree: e.detail.value
    })
    this.data.Classify = {
      CoditionName: "ClassifyGuid",
      CoditionType: "nvarchar",
      CoditionValue: [this.data.classify_Data[e.detail.value].RowGuid],
    }
    console.log(JSON.stringify(this.data.Classify) + "______________设备三名称")
  },

  equipmentOneThree: function() { //设备分类三
    var that = this;
    that.setData({
      classify_Data: [{
        RowGuid: '0',
        ClassifyName: '请选择'
      }],
      indexThree: 0
    })
    var indexTwo = that.data.indexTwo;
    var sysType_Data = that.data.sysType_Data;
    console.log(sysType_Data[indexTwo].RowGuid)
    if (sysType_Data[indexTwo].SystemName == "请选择") {
      that.setData({
        classify_Data: [{
          RowGuid: '0',
          ClassifyName: '请选择'
        }],
        indexThree: 0
      })
    }
    var params = {
      ParentGuid: sysType_Data[indexTwo].RowGuid
    }
    httpRequest.GetValidateRequest(httpRequest.apiInfo.GetMaterClassifyDictionaryApi, params, function(data) {
      console.log(JSON.stringify(data) + "_______________设备分类三")
      if (data.length > 0) {
        var newarr = that.data.classify_Data
        for (var i = 0; i < data.length; i++) {
          var param = {
            RowGuid: data[i].RowGuid,
            ClassifyName: data[i].ClassifyName
          }
          newarr.push(param)
        }
        that.setData({
          classify_Data: newarr
        })
      }
    }, function(err) {})
  },

  // 点击左上角小图标事件
  tap_ch: function(e) {
    console.log("发送")
    if (this.data.open) {
      this.setData({
        open: false,
        show: false
      });
      setTimeout(() => {
        this.setData({
          Masks: false
        })
      }, 300)
    } else {
      this.setData({
        open: true,
        Masks: true
      });
      setTimeout(() => {
        this.setData({
          show: true
        })
      }, 300)
    }
  },

  // tap_start: function(e) {
  //   // touchstart事件
  //   // 把手指触摸屏幕的那一个点的 x 轴坐标赋值给 mark 和 newmark
  //   this.data.mark = this.data.newmark = e.touches[0].pageX;
  // },

  // tap_drag: function(e) {
  //   // touchmove事件
  //   this.data.newmark = e.touches[0].pageX;

  //   // 手指从左向右移动
  //   if (this.data.mark < this.data.newmark) {
  //     this.istoright = true;
  //     this.setData({
  //       Masks: true
  //     })
  //   }

  //   // 手指从右向左移动
  //   if (this.data.mark > this.data.newmark) {
  //     this.istoright = false;
  //     this.setData({
  //       Masks: false
  //     })
  //   }
  //   this.data.mark = this.data.newmark;
  // },

  tap_end: function(e) {
    // touchend事件
    this.data.mark = 0;
    this.data.newmark = 0;
    // 通过改变 opne 的值，让主页加上滑动的样式
    if (this.istoright) {
      this.setData({
        open: true,
        Masks: true,
        show: true
      });
    } else {
      this.setData({
        open: false,
        show: false
      });
      setTimeout(() => {
        this.setData({
          Masks: false
        })
      }, 300)
    }
  },

  //跳转设备详情
  navEquipDetail: function(e) {
    console.log('跳转设备详情', e)
    wx.navigateTo({
      url: '/pages/equip_detail/equip_detail?RowGuid=' + e.currentTarget.dataset.rowguid + '&EquipmentCode=' + e.currentTarget.dataset.equipmentcode + '&EquipmentName=' + e.currentTarget.dataset.equipmentname + '&havefssb=' + this.data.havefssb
    })
  },

  bindRegionChange: function(e) {
    this.setData({
      RegionIndex: e.detail.value
    })
    this.data.Region = {
      CoditionName: "RegionGuid",
      CoditionType: "nvarchar",
      CoditionValue: [this.data.RegionArray[e.detail.value].RowGuid],
    }
    console.log(JSON.stringify(this.data.Region) + "______________区域名称")
    this.getBuilding();
  },

  //获取所属区域
  getRegion: function() {
    var that = this
    httpRequest.GetValidateRequest(httpRequest.apiInfo.Project_GetProjectRegionApi, null,
      function(res) {
        console.log(JSON.stringify(res))
        if (res.length > 0) {
          var newArray = that.data.RegionArray
          for (var i = 0; i < res.length; i++) {
            newArray.push(res[i])
          }
          that.setData({
            RegionArray: newArray
          })
        }
      },
      function(err) {
        console.error(err)
      })
  },

  bindBuildingChange: function(e) {

    this.setData({
      BuildingIndex: e.detail.value
    })
    this.data.Build = {
      CoditionName: "BuildingGuid",
      CoditionType: "nvarchar",
      CoditionValue: [this.data.BuildingArray[e.detail.value].RowGuid],
    }
    console.log(JSON.stringify(this.data.Build) + "_________________建筑名称")
    this.getFloor();
  },

  //获取所属建筑
  getBuilding: function() {
    var that = this
    that.setData({
      BuildingArray: [{
        RowGuid: '0',
        BuildingName: '请选择',
        BuildingCode: '00'
      }],
      BuildingIndex: 0,
      FloorArray: [{
        RowGuid: '0',
        FloorName: '请选择',
        FloorCode: '00'
      }],
      FloorIndex: 0,
      RoomArray: [{
        RowGuid: '0',
        Room: '请选择'
      }],
      RoomIndex: 0
    })
    var RegionIndex = that.data.RegionIndex;
    var RegionArray = that.data.RegionArray;
    console.log(RegionArray[RegionIndex].RowGuid)
    if (RegionArray[RegionIndex].RegionName == "请选择") {
      that.setData({
        BuildingArray: [{
          RowGuid: '0',
          BuildingName: '请选择',
          BuildingCode: '00'
        }],
        BuildingIndex: 0
      })
    } else {
      var param = {
        RegionGuid: RegionArray[RegionIndex].RowGuid
      };
      httpRequest.GetValidateRequest(httpRequest.apiInfo.Project_GetProjectBuildingApi, param,
        function(res) {
          console.log(JSON.stringify(res))
          if (res.length > 0) {
            var newArray = that.data.BuildingArray
            for (var i = 0; i < res.length; i++) {
              newArray.push(res[i])
            }
            that.setData({
              BuildingArray: newArray
            })
          }
        },
        function(err) {
          console.error(err)
        })
    }
  },

  bindFloorChange: function(e) {

    this.setData({
      FloorIndex: e.detail.value
    })
    this.data.Floor = {
      CoditionName: "FloorGuid",
      CoditionType: "nvarchar",
      CoditionValue: [this.data.FloorArray[e.detail.value].RowGuid],
    }
    console.log(JSON.stringify(this.data.Floor) + "______________楼层名称")
    this.getRoom();
  },

  //获取所属楼层
  getFloor: function() {
    var that = this
    that.setData({
      FloorArray: [{
        RowGuid: '0',
        FloorName: '请选择',
        FloorCode: '00'
      }],
      FloorIndex: 0,
      RoomArray: [{
        RowGuid: '0',
        Room: '请选择'
      }],
      RoomIndex: 0
    })
    var BuildingIndex = that.data.BuildingIndex;
    var BuildingArray = that.data.BuildingArray;
    console.log(BuildingArray[BuildingIndex].RowGuid)
    if (BuildingArray[BuildingIndex].RegionName == "请选择") {
      that.setData({
        FloorArray: [{
          RowGuid: '0',
          FloorName: '请选择',
          FloorCode: '00'
        }],
        FloorIndex: 0
      })
    } else {
      var param = {
        BuildingGuid: BuildingArray[BuildingIndex].RowGuid
      };
      httpRequest.GetValidateRequest(httpRequest.apiInfo.Project_GetProjectFloorApi, param,
        function(res) {
          console.log(JSON.stringify(res))
          if (res.length > 0) {
            var newArray = that.data.FloorArray
            for (var i = 0; i < res.length; i++) {
              newArray.push(res[i])
            }
            that.setData({
              FloorArray: newArray
            })
          }
        },
        function(err) {
          console.error(err)
        })
    }
  },

  bindRoomChange: function(e) {

    this.setData({
      RoomIndex: e.detail.value
    })
    this.data.Rooms = {
      CoditionName: "RoomGuid",
      CoditionType: "nvarchar",
      CoditionValue: [this.data.RoomArray[e.detail.value].RowGuid],
    }
    console.log(JSON.stringify(this.data.Rooms) + "________________房间名称")
  },

  //获取所属房间
  getRoom: function() {
    var that = this
    that.setData({
      RoomArray: [{
        RowGuid: '0',
        Room: '请选择'
      }],
      RoomIndex: 0
    })
    var FloorIndex = that.data.FloorIndex;
    var FloorArray = that.data.FloorArray;
    console.log(FloorArray[FloorIndex].RowGuid)
    if (FloorArray[FloorIndex].FloorName == "请选择") {
      that.setData({
        RoomArray: [{
          RowGuid: '0',
          Room: '请选择'
        }],
        RoomIndex: 0
      })
    } else {
      var param = {
        FloorGuid: FloorArray[FloorIndex].RowGuid
      };
      httpRequest.GetValidateRequest(httpRequest.apiInfo.Project_GetProjectRoomApi, param,
        function(res) {
          console.log(JSON.stringify(res))
          if (res.length > 0) {
            var newArray = that.data.RoomArray
            for (var i = 0; i < res.length; i++) {
              newArray.push(res[i])
            }
            that.setData({
              RoomArray: newArray
            })
          }
        },
        function(err) {
          console.error(err)
        })
    }
  },

  inputInfo: function(e) {
    this.data.EquipmentName = {
      CoditionName: "EquipmentName",
      CoditionType: "nvarchar",
      CoditionValue: [e.detail.value],
    }
    console.log(JSON.stringify(this.data.EquipmentName) + "____________输入框")
  },

  Search: function() { //筛选
    this.setData({
      listArray: [],
      pagesize: 15,
      counter: 1,
      scrollTop:0
    })
    this.initData();
  },

  Reset: function() { //重置
    this.setData({
      index: 0, //picker下标
      sys_Data: [{
        RowGuid: '0',
        ParentGuid: '0',
        SystemName: '请选择'
      }], //设备分类一
      sysType_Data: [{
        RowGuid: '0',
        SystemName: '请选择'
      }], //设备分类二
      indexTwo: 0, //分类二下标
      classify_Data: [{
        RowGuid: '0',
        ClassifyName: '请选择'
      }], //分类三
      indexThree: 0, //分类三下标
      RegionArray: [{
        RowGuid: '0',
        RegionName: '请选择',
        RegionCode: '00'
      }], //区域
      RegionIndex: 0,
      BuildingArray: [{
        RowGuid: '0',
        BuildingName: '请选择',
        BuildingCode: '00'
      }], //建筑
      BuildingIndex: 0,
      FloorArray: [{
        RowGuid: '0',
        FloorName: '请选择',
        FloorCode: '00'
      }], //楼层
      FloorIndex: 0,
      RoomArray: [{
        RowGuid: '0',
        Room: '请选择'
      }], //房间
      RoomIndex: 0,
      Region: {
        CoditionName: "RegionGuid",
        CoditionType: "nvarchar",
        CoditionValue: [""],
      }, //区域值
      Build: {
        CoditionName: "BuildingGuid",
        CoditionType: "nvarchar",
        CoditionValue: [""],
      }, //建筑值
      Floor: {
        CoditionName: "FloorGuid",
        CoditionType: "nvarchar",
        CoditionValue: [""],
      }, //楼层值
      Rooms: {
        CoditionName: "RoomGuid",
        CoditionType: "nvarchar",
        CoditionValue: [""],
      }, //房间值
      SysInfo: {
        CoditionName: "SysGuid",
        CoditionType: "nvarchar",
        CoditionValue: [""],
      }, //设备一值
      SysType: {
        CoditionName: "Sys_TypeGuid",
        CoditionType: "nvarchar",
        CoditionValue: [""],
      }, //设备二值
      Classify: {
        CoditionName: "ClassifyGuid",
        CoditionType: "nvarchar",
        CoditionValue: [""],
      }, //设备三值
      EquipmentName: {
        CoditionName: "EquipmentName",
        CoditionType: "nvarchar",
        CoditionValue: [""],
      }, //输入框值
      val: ''
    })
    this.equipmentOne();
    this.getRegion();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    this.setData({
      customHeight: (app.globalData.titleBarHeight + app.globalData.statusBarHeight) * 2
    })

    console.log('附属设备跳转过来', options)
    this.setData({
      CustomBar: app.globalData.statusBarHeight + app.globalData.titleBarHeight
    })
    if (options.ParentGuid != undefined && options.ParentGuid != '' && options.EquipmentName != undefined && options.EquipmentName != '' &&
      options.havefssb != undefined && options.havefssb != '') {
      this.setData({
        ParentGuid: options.ParentGuid,
        title: '附属设备',
        havefssb: 1
      })
    }
    this.initData();
    this.refreshView = this.selectComponent("#refreshView")


    wx.getSystemInfo({
      success: e => {
        let custom = wx.getMenuButtonBoundingClientRect();
        this.setData({
          CustomBar: custom.bottom + custom.top - e.statusBarHeight
        })
      }
    })



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },


  //触摸开始
  handletouchstart: function(event) {
    this.refreshView.handletouchstart(event)
  },
  //触摸移动
  handletouchmove: function(event) {
    this.refreshView.handletouchmove(event)
  },
  //触摸结束
  handletouchend: function(event) {
    this.refreshView.handletouchend(event)
  },
  //触摸取消
  handletouchcancel: function(event) {
    this.refreshView.handletouchcancel(event)
  },
  //页面滚动
  onPageScroll: function(event) {
    this.refreshView.onPageScroll(event)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    setTimeout(() => {
      this.setData({
        uploading: true,
        noMore: false,
        pagesize: 15,
        counter: 1,
        listArray: []
      })
      this.initData();
      this.refreshView.stopPullRefresh()
    }, 1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})